//
//  GRRemotePlayerFileManager.m
//  GRFMRemotePlayer_Example
//
//  Created by GR on 2018/1/17.
//  Copyright © 2018年 guorui. All rights reserved.
//

#import "GRRemotePlayerFileManager.h"
#import <MobileCoreServices/MobileCoreServices.h>

#define kCachePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"downloadCompletion"]
#define kTmpPath NSTemporaryDirectory()

@implementation GRRemotePlayerFileManager
+ (NSString *)cachePathWithURL:(NSURL *)url
{
    return [kCachePath stringByAppendingPathComponent:url.lastPathComponent];
}

+ (BOOL)cacheExistsWithURL:(NSURL *)url
{
    if (url == nil) {
        return NO;
    }
    NSString *cachePath = [self cachePathWithURL:url];
    return [[NSFileManager defaultManager] fileExistsAtPath:cachePath];
}

+ (long long)cacheSizeWithURL:(NSURL *)url
{
    if (![self cacheExistsWithURL:url]) {
        return 0;
    }
    
    NSString *cachePath = [self cachePathWithURL:url];
    NSDictionary *infoDict = [[NSFileManager defaultManager] attributesOfItemAtPath:cachePath error:nil];
    return [infoDict[NSFileSize] longLongValue];
}

+ (NSString *)contentTypeWithURL:(NSURL *)url
{
    if (![self cacheExistsWithURL:url]) {
        return nil;
    }
    
    NSString *path = [self cachePathWithURL:url];
    NSString *fileExtension = path.pathExtension;
    
    CFStringRef contentTypeCF = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef _Nonnull)(fileExtension), NULL);
    NSString *contentType = CFBridgingRelease(contentTypeCF);
    
    return contentType;
}

+ (NSString *)tmpPathWithURL:(NSURL *)url
{
    return  [kTmpPath stringByAppendingPathComponent:url.lastPathComponent];
}

+ (BOOL)tmpExistsWithURL:(NSURL *)url
{
    if (url == nil) {
        return NO;
    }
    NSString *tmpPath = [self tmpPathWithURL:url];
    return [[NSFileManager defaultManager] fileExistsAtPath:tmpPath];
}

+ (long long)tmpSizeWithURL:(NSURL *)url
{
    if (![self tmpExistsWithURL:url]) {
        return 0;
    }
    
    NSString *tmpPath = [self tmpPathWithURL:url];
    NSDictionary *infoDict = [[NSFileManager defaultManager] attributesOfItemAtPath:tmpPath error:nil];
    return [infoDict[NSFileSize] longLongValue];
}

+ (void)moveToCacheWithURL:(NSURL *)url
{
    if (![self tmpExistsWithURL:url]) {
        return;
    }
    
    NSString *tmpPath = [self tmpPathWithURL:url];
    NSString *cachePath = [self cachePathWithURL:url];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:kCachePath] == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:kCachePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    [[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:cachePath error:nil];
}

+ (void)removeTmpWithURL:(NSURL *)url
{
    if (![self tmpExistsWithURL:url]) {
        return;
    }
    
    NSString *tmpPath = [self tmpPathWithURL:url];
    [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];
}

@end
